package com.rmdjzz.slmmn.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.rmdjzz.slmmn.pojo.base.Love;
import com.rmdjzz.slmmn.pojo.base.Song;
import com.rmdjzz.slmmn.pojo.vo.SongVo;
import com.rmdjzz.slmmn.service.SingerService;
import com.rmdjzz.slmmn.service.SongService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @作者 张绍鑫
 * @创建时间 2023/6/7
 */
@Api(tags = "基本业务逻辑")
@Controller
@Slf4j
public class Main {
    @Autowired
    private SongService songService;
    @Autowired
    private SingerService singerService;
    @RequestMapping("/logOut")
    @ApiOperation("用户登出")
    @ResponseBody
    public void logOut(HttpSession session){
        session.invalidate();
    }
    @PostMapping("/listen/{songId}")
    @ApiOperation("听歌改库")
    @ResponseBody
    public void listen(@PathVariable Integer songId){
        Song song = songService.getById(songId);
        song.setNum(song.getNum()+1);
        song.setHeat(song.getHeat()+1);
        songService.updateById(song);
        log.info("添加播放次数");
    }
    @GetMapping("/userSearchSong/{sc}")
    @ApiOperation("用户界面模糊查询")
    public String userSearchSong(@PathVariable String sc, Model model){
        QueryWrapper<Song> songQueryWrapper = new QueryWrapper<>();
        songQueryWrapper.like("song_name",sc);
        List<Song> records = songService.list(songQueryWrapper);
        ArrayList<SongVo> songVos = new ArrayList<>();
        ArrayList<String> words = new ArrayList<>();
        for (Song song:records){
            SongVo songVo = new SongVo();
            songVo.setId(song.getId());
            songVo.setSongImg(song.getSongImg());

            songVo.setWords(song.getWords());
            songVo.setSongName(song.getSongName());
            songVo.setSongTime(song.getSongTime());
            if (singerService.getById(song.getSingerId())==null){
                songVo.setSingerName("未知歌手");
            }else {
                songVo.setSingerName(singerService.getById(song.getSingerId()).getSingerName());
            }
            songVo.setSong(song.getSong());
            log.info("song："+song);
            log.info("songVo："+songVo);
            words.add(song.getWords());
            songVos.add(songVo);
        }
        model.addAttribute("list",songVos);
        model.addAttribute("words",words);
        model.addAttribute("msg","查找歌曲");
        return "play";
    }

    @GetMapping("/recruit")
    public String recruit(){
        return "recruit";
    }
    @GetMapping("/customer")
    public String customer(){
        return "Customer";
    }
    @GetMapping("/aboutUs")
    public String aboutUs(){
        return "about us";
    }

}
